{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 文档嵌入模型的使用\n",
    "\n",
    "① 向量化一个句子（即字符串）：embed_query"
   ],
   "id": "98d66f104976ee84"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-06T03:04:44.929047Z",
     "start_time": "2025-08-06T03:04:44.031491Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "import os\n",
    "import dotenv\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "# 初始化嵌入模型\n",
    "embeddings_model = OpenAIEmbeddings(model=\"text-embedding-ada-002\")\n",
    "# embeddings_model = OpenAIEmbeddings(model=\"text-embedding-3-large\")\n",
    "\n",
    "# 待嵌入的文本句子\n",
    "text = \"What was the name mentioned in the conversation?\"\n",
    "\n",
    "# 生成一个嵌入向量，得到一个浮点类型变量构成数组\n",
    "embedded_query = embeddings_model.embed_query(text = text)\n",
    "\n",
    "# 使用embedded_query[:5]来查看前5个元素的值\n",
    "print(embedded_query[:5])\n",
    "\n",
    "# print(len(embedded_query))  #1536 -> 3072"
   ],
   "id": "dc350f217bcb6d37",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.005329647101461887, -0.0006122003542259336, 0.0389961302280426, -0.002898985054343939, -0.008904732763767242]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "① 向量化一个文档（即document）：embed_documents",
   "id": "ef2c1b773341f08f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-06T03:06:20.793269Z",
     "start_time": "2025-08-06T03:06:19.336232Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import dotenv\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "# 初始化嵌入模型\n",
    "embeddings_model = OpenAIEmbeddings(model=\"text-embedding-ada-002\")\n",
    "\n",
    "# 待嵌入的文本列表\n",
    "texts = [\n",
    "    \"Hi there!\",\n",
    "    \"Oh, hello!\",\n",
    "    \"What's your name?\",\n",
    "    \"My friends call me World\",\n",
    "    \"Hello World!\"\n",
    "]\n",
    "\n",
    "# 生成嵌入向量\n",
    "embeddings = embeddings_model.embed_documents(texts)\n",
    "\n",
    "\n",
    "for i in range(len(texts)):\n",
    "    print(f\"{texts[i]}:{embeddings[i][:3]}\",end=\"\\n\\n\")\n"
   ],
   "id": "6f62b4b19b381a8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi there!:[-0.020325319841504097, -0.007096723187714815, -0.022839006036520004]\n",
      "\n",
      "Oh, hello!:[0.00445469468832016, -0.014359182678163052, 0.0019080477068200707]\n",
      "\n",
      "What's your name?:[-0.00477176159620285, -0.009507440961897373, 0.00713208457455039]\n",
      "\n",
      "My friends call me World:[-0.004583988804370165, -0.014502654783427715, 0.010228524915874004]\n",
      "\n",
      "Hello World!:[0.002363691572099924, 0.00023463694378733635, -0.00233377143740654]\n",
      "\n"
     ]
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
